package com.woniu.util;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.response.AlipayTradeQueryResponse;
import org.springframework.stereotype.Component;



@Component
public class PayUtil {

    //appid
    private final String APP_ID = "9021000152653836";
    //应用私钥
    private final String APP_PRIVATE_KEY = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDE36gy229TsASViqefTDAZn+hCQa7wUx8+aD5Zn8GLT0/nompME35zy7D9G6kJbLf3w238V6aMvBd1xui7l5KAy22v5llsjURjq6eiwBl/eHxLvavGqtM69lNl+iTxxwGRXWZjEou1I1FiiHrwimYem6jFf2bubCJEoDJN1NBX7h5vQ186W9LNMyLAJbHYnR3KDYU4PvapyrE0v0RPswhclvA0d8vKyldXtIcuhFCVELRTbYH3Ul8D9jEblj2MJraJwPDMCphovISQIuMYh8I+BGy7DiDwHVvIDo3/6+hzrrvcrUuACDXwLNDGB+++wLqyqvQgr04EFUmjIXVViCoxAgMBAAECggEAFS5m/0nCmYNnittHG4lr52Xf7Dj65FpdEy7N/oCEp0NaOnar7xFt2nGA0kqGjkAHLv30uGCJx7x43bAP3Bb7qSW54GHtHxO31u1h9M2sMJ266b83WwGIBmD4KhGWpfieP5CH8oLNSyjImcxLRdPuFr7ykoUZgu9ZZH7DysHpxHvmrRbbRe9blTF2P0AJm/OOKKd5JBdD2falcx6nT+tSw8e0kqGolLxOnjdMbUWM6SibQWOv9iQ1+TzJC63vdjJpyiSPaYHIkjFK0N+Pfrd2eizHpcRrnUiMsp9uTAlaZMmmn5pIhDATwLcccj5EP59kqkSC/G49N4W7AfLgD0nO/QKBgQDpAMR4br7sI7FIeA3+tRuaaWUbYVpLshWlvpD9HJR3z0JNxMtnEHInY1HGvcBssOyzKEHFPaLDIbCl4FN5Uc7irmubPZU94kUuDVmJ/MW1Qc/rV8u4fzw40Y9jkRQMmuwAUcaKFveWQaikauslB+msSVw7Ez7dhXxzGu+S/GocYwKBgQDYTgSbUiS01D0k3c/afuu09xc6w+PVupWpHV7OBdJU5WTjH3aK6JE2b0BYgmlC9AY3xj1en8GvgndrVk9lbP7plZyDC+2QHXi3oDe+d52zUf0uHbqtzEIKVy1nz50ih/nndvjnlpJ+53NSLMJMFGp8+mt3/hwJ5l0z7M44rhSRWwKBgQDcQ693simmje9P9L42JPtdM3kzMwrrOASi1M23mUb5I+MoJXuriK6BcbCi0kqP9YktrEbQsP2mvbiCdtgyfhW30O9EqCjngXmomVIl0eENApQNhMe/N/i0Ndb5ZvKvH90uSCAJ9sBDOPq1w6VbMmnCRu97oSvyuff10GbeauPcxQKBgFB6trwTlBSz4wwUe4GQN6LdTIdQ2zRqd8S0aNG+2MM9lovq6PBGF8FpI0fLPd7rbGTUHYFXb3kd0dDeDCxwoWfHgrQ/e+RjEsc/lX4Ig2R03P6LkSmZFflC9mHpnU17yj+UxIdmPpt5Ai9Q9/nWnshhZBsBMO1+CK+JLDFqk7yBAoGBAOVC+hRKbEW4PSlJZ4NdLPizZNoXD6vgsFDozgsfS9YiAYBdO3GmdriNa4XFnB4QsHqGsW141Xr5eOyWGV9rNgsUwek1uHllTRC1XAzbPPcOAecMCm0+Jmk5Dpd8zmJSk0WP6ZE52pL8pLw2/OLWiT3vds09kcIhGXLZrs9mOei7";

    private final String CHARSET = "UTF-8";
    // 支付宝公钥
    private final String ALIPAY_PUBLIC_KEY = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAwYfcgt+mqBsuiMd72kJsXrrDpMPHjy5rBKXfb44MWpkpjsEfJHSokK0gdgfDLf68itNVPwEh2Qgd0AV3/boANLZ3PgvCL55MYf9akC6Ztdbdbb7hCQkIg0yKIielVT1Iv/iFJ3Yl3fBeC6b75AnBnK2lXnyIX3T62/K3jnDKoNwv1DcUuEsLG6UCnDISYaklXVp5NVDg6JLUBYsah5Hd1ePoWhiafTeaP5vzCGwcEgFAEiu+fa0Xh/cfHlsCzK4f3iwvT8+kajLIJ2BN/gmNkTjdn2HSwP3BjWKJ4j1oYVT1dUeJD1JW038oi7wB2KxsWc5NE8K15JADZrl4wmWrrwIDAQAB";
    //这是沙箱接口路径,正式路径为https://openapi.alipay.com/gateway.do
    private final String GATEWAY_URL = "https://openapi-sandbox.dl.alipaydev.com/gateway.do";
    private final String FORMAT = "JSON";
    //签名方式
    private final String SIGN_TYPE = "RSA2";
    //支付宝异步通知路径,付款完毕后会异步调用本项目的方法,必须为公网地址
    private final String NOTIFY_URL = "http://jc5cc836.natappfree.cc/alipay/toSuccess";
    //支付宝同步通知路径,也就是当付款完毕后跳转本项目的页面,可以不是公网地址
    private final String RETURN_URL = "http://localhost:8080/alipay/toSuccess";
    private AlipayClient alipayClient = null;

    //支付宝官方提供的接口
    public String sendRequestToAlipay(String outTradeNo, Float totalAmount, String subject) throws AlipayApiException {
        //获得初始化的AlipayClient
        alipayClient = new DefaultAlipayClient(GATEWAY_URL, APP_ID, APP_PRIVATE_KEY, FORMAT, CHARSET, ALIPAY_PUBLIC_KEY, SIGN_TYPE);

        //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(RETURN_URL);
        alipayRequest.setNotifyUrl(NOTIFY_URL);

        //商品描述（可空）
        String body = "";
        alipayRequest.setBizContent("{\"out_trade_no\":\"" + outTradeNo + "\","
                + "\"total_amount\":\"" + totalAmount + "\","
                + "\"subject\":\"" + subject + "\","
                + "\"body\":\"" + body + "\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        //请求
        String result = alipayClient.pageExecute(alipayRequest).getBody();
        System.out.println("返回的结果是：" + result);
        return result;
    }

    //    通过订单编号查询
    public String query(String id) {
        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
        JSONObject bizContent = new JSONObject();
        bizContent.put("out_trade_no", id);
        request.setBizContent(bizContent.toString());
        AlipayTradeQueryResponse response = null;
        String body = null;
        try {
            response = alipayClient.execute(request);
            body = response.getBody();
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        if (response.isSuccess()) {
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
        }
        return body;
    }


}
